package flow.action;

import flow.model.FlowInstance;
import flow.model.FlowInstanceSection;
import flow.service.FlowInstanceService;
import flow.service.IFlowService;
import m.appserver.ActionRedirect;
import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryOrder;
import m.common.service.Service;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.list.*;
import m.system.util.ClassUtil;
import m.system.util.JSONMessage;
import manage.action.ManageAction;

@ActionMeta(name="flow/flowInstance")
public class FlowInstanceAction extends ManageAction {
	private FlowInstance model;

	public Object toProcess() throws Exception {
		model= ModelQueryList.getModel(model,"*","flowDefine.*","business.*","currentSection.*");
		ActionRedirect ar=((IFlowService)getService((Class<Service>) ClassUtil.getClass(model.getBusiness().getBusiService())))
				.getProcessActionRedirect(model,getSessionAdminOid());
		return getResponse().redirect(ar);
	}
	public Object toView() throws Exception {
		model= ModelQueryList.getModel(model,"*","flowDefine.*","business.*","currentSection.*");
		ActionRedirect ar=((IFlowService)getService((Class<Service>) ClassUtil.getClass(model.getBusiness().getBusiService())))
				.getViewActionRedirect(model,getSessionAdminOid());
		return getResponse().redirect(ar);
	}
	public JSONMessage getInstance() {
		JSONMessage result=new JSONMessage();
		try {
			result=getService(FlowInstanceService.class).getInstance(model);
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}

	public VueResult flowInstanceList() throws Exception{
		verifyMenu("A3111");
		boolean power=hasAdminOperPower("manage_flow_power");
		VueListResult result=getVueListResult("流程实例",FlowInstance.class,"action/flow/flowInstance/flowInstanceList");
		result.setSubTitle("流程实例列表");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("业务类型", "business.type",true).setSort(true)
			.setStyle("record.doneStatus=='Y'", "color:#0a0;").setStyle("record.doneStatus=='N'", "color:#f00;"));
		result.addCol(toLinkColumn("业务标题", "business.title",230,true,
			new ListEvent("action/flow/flowInstance/toView", EventType.MODAL,1200).addParam("model.oid","oid")
		));
		result.addCol(toLinkColumn("流程", "flowDefine.identity",200,true,
			new ListEvent("action/flow/flowDefine/toEdit", EventType.MODAL,800).addParam("model.oid","flowDefine.oid")
		).setFieldExpression("concat(#{flowDefine.name},'(',#{flowDefine.identity},')')"));
		result.addCol(toTextColumn("当前环节", "currentSection.identity",150,true).setFieldExpression("concat(#{currentSection.identity},'(',#{currentSection.name},')')"));
		result.addCol(toDateColumn("创建时间", "createDate",true).setSort(true));
		result.addCol(toTextColumn("完成状态", "doneStatus",true).setAlign(Align.CENTER).setDictOption(FlowInstance.class,"doneStatus")
			.setStyle("text=='Y'", "color:#0a0;").setStyle("text=='N'", "color:#f00;"));
		result.addCol(toDateColumn("完成时间", "doneDate",true).setSort(true));
		if(power) result.addCol(toSpaceColumn("操作",180,
			ListButton.modal("执行情况", ButtonType.PRIMARY,"action/flow/flowInstance/flowInstanceSectionList",700, SuccessMethod.REFRESH)
				.addParam("params[flowInstance.oid]","oid"),
			ListButton.modal("流程实例",ButtonType.PRIMARY,"page/base/flow/viewFlowDefine.js",800,SuccessMethod.REFRESH)
				.addParam("instanceOid","oid").addParam("defineOid","flowDefine.oid")
		));
		return result;
	}

	public VueResult flowInstanceSectionList() throws Exception{
		verifyMenu("A3111");
		VueListResult result=getVueListResult("流程实例环节",FlowInstanceSection.class,"action/flow/flowInstance/flowInstanceSectionList");
		result.setSubTitle("流程实例环节列表");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("索引", "flowIndex",60).setSort(ListSortOrder.ASC));
		result.addCol(toTextColumn("环节", "flowSection.identity",150,true).setFieldExpression("concat(#{flowSection.identity},'(',#{flowSection.name},')')"));
		result.addCol(toTextColumn("处理人", "user.realname",90,true));
		result.addCol(toDateColumn("接收时间", "acceptDate",true).setSort(true));
		result.addCol(toTextColumn("完成状态", "doneStatus",true).setAlign(Align.CENTER).setDictOption(FlowInstanceSection.class,"doneStatus"));
		result.addCol(toDateColumn("完成时间", "doneDate",true).setSort(true));
		result.addSearch(ListSearch.field("","flowInstance.oid", SearchType.HIDDEN_EQ));
		result.addOrder(QueryOrder.desc("doneDate"));
		return result;
	}

	public FlowInstance getModel() {
		return model;
	}

	public void setModel(FlowInstance model) {
		this.model = model;
	}
	
}
